// pages/exam/examJoin/examJoin.js
var app = getApp();
var config = require('../../../config.js');
// const { provinces } = require('../../../mock.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: false,
    tihuoWay: "",
    select_2: false,
    secondName: "",
    select_3: false,
    thirdName: "",
    inputUserName: null,
    inputUserPhone: null,
    inputUserUnit: null,
    firstList: null,
    secondList: null,
    unitName: null,


    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo: ''


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var examId = options.examId
    this.setData({
      examId: examId
    })
    this.getData();
  },

  //获取一二级单位
  getData: function () {
    var that = this;
    wx.request({
      url: config.getExamUnit,
      method: "POST",
      data: {
        examId: that.data.examId
      },
      success: function (ret) {
        var data = ret.data
        if (data.success == 0) {
          var list = data.rows;
          handler(result.rows, depth);
          // if (list) {
          //   that.setData({
          //     firstList: list
          //   });
          // }
        }
      }
    })
  },


 handler:function(data, depth, parent_id, parent_name) {
    if (Array.isArray(data)) {
      data.forEach(({ id, unitName, examUnits }) => {
        switch (true) {
          case depth === 1:
            provinces.push({ id, name: unitName });
            break;
          case depth === 2:
            if (!citys[parent_id]) citys[parent_id] = [];
            citys[parent_id].push({ id, name: unitName, province: parent_name });
            break;
          case depth === 3:
            if (!areas[parent_id]) areas[parent_id] = [];
            areas[parent_id].push({ id, name: unitName, province: parent_name });
            break;
          default:
            break;
        }
        if (Array.isArray(examUnits)) handler(examUnits, depth + 1, id, unitName);
      });
    }
  },









  //根据一级单位查找二级单位集合
  getSecondListById: function (id, list) {
    var that = this;
    if (!list || !id) {
      return;
    }

    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      if (id == item.id) {
        // var secondList = item.examUnits;
        // return secondList;
        var citys = item.examUnits;
        return citys;
      }
    }

    return null;
  },


  //根据二级单位查找三级单位集合
  getThirdListById: function (id, list) {
    var that = this;
    if (!list || !id) {
      return;
    }

    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      if (id == item.id) {
        var thridList = item.examUnits;
        return thridList;
      }
    }
    return null;
  },



  bindFirstList: function (e) {
    var that = this;
    that.setData({
      select: !this.data.select
    })
  },
  mySelect: function (e) {
    var that = this;
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    that.setData({
      tihuoWay: name,
      firstUnitId: id,
      select: false
    })
    //动态从集合获取对应二级列表

    var list = this.getSecondListById(id, that.data.provinces);
    console.log("一级单位 ---" + JSON.stringify(that.data.provinces));
    console.log(" yi ji dan wei id ---" + id);
    if (list) {
      console.log("er ji dan wei ---" + JSON.stringify(list));
      that.setData({
        secondList: list
      })
    }


  },

  bindSecondList: function (e) {
    var that = this;
    that.setData({
      select_2: !this.data.select_2
    })
  },
  mySecondSelect: function (e) {
    var that = this;
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    that.setData({
      secondName: name,
      secondUnitId: id,
      select_2: false
    })

    //动态从集合获取对应三级列表

    var list2 = this.getThirdListById(id, that.data.secondList);
    console.log("二级 ---" + JSON.stringify(that.data.secondList));
    console.log("san ji dan wei ---" + JSON.stringify(list2));
    if (list2) {

      that.setData({
        thridList: list2
      })
    }
  },


  bindThridList: function (e) {
    var that = this;
    that.setData({
      select_3: !this.data.select_3
    })
  },
  myThridSelect: function (e) {
    var that = this;
    var name = e.currentTarget.dataset.name
    var id = e.currentTarget.dataset.id
    that.setData({
      thirdName: name,
      thridUnitId: id,
      select_3: false
    })
  },

  //开始考试
  startExam: function (e) {
    var that = this;
    console.log("tihuoWay===" + that.data.tihuoWay);
    console.log("secondName===" + that.data.secondName);
    console.log("thirdName===" + that.data.thirdName);
    console.log("inputUserName===" + that.data.inputUserName);
    console.log("inputUserPhone===" + that.data.inputUserPhone);
    console.log("inputUserUnit===" + that.data.inputUserUnit);
    if (!that.data.tihuoWay) {
      wx.showToast({
        title: '请选择一级单位',
        icon: "none"
      })
      return;
    }
    if (!that.data.secondName) {
      wx.showToast({
        title: '请选择二级单位',
        icon: "none"
      })
      return;
    }
    if (!that.data.thirdName) {
      wx.showToast({
        title: '请选择三级单位',
        icon: "none"
      })
      return;
    }

    if (!that.data.inputUserName) {
      wx.showToast({
        title: '请输入您的名字',
        icon: "none"
      })
      return;

    }

    if (!that.data.inputUserPhone) {
      wx.showToast({
        title: '请输入您的手机号',
        icon: "none"
      })
      return;

    }
    var phoneRexp = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    if (!phoneRexp.test(that.data.inputUserPhone)) {

      wx.showToast({
        title: '您的手机号有误',
        icon: "none"
      })
      return;
    }
  this.postExamData(that.data.firstUnitId, that.data.secondUnitId,that.data.thridUnitId,that.data.inputUserName, that.data.inputUserPhone, that.data.inputUserUnit);


  },

  //提交数据
  postExamData: function (firstUnit, secondUnit, thirdUnit, userName, phone, unitName) {
    var that = this;
    wx.request({
      url: config.startExam,
      method: "POST",
      data: {
        examId: that.data.examId,
        openId: app.globalData.openid,
        unitOneId: firstUnit,
        unitTwoId: secondUnit,
        unitThreeId:thirdUnit,
        userName: userName,
        phone: phone,
        unitName: unitName

      },
      success: function (ret) {
        var data = ret.data
        if (data.success == 0) {
          //跳转到倒计时页面
          wx.redirectTo({
            url: '../examLoading/examLoading?examId=' + that.data.examId
          })
        } else {
          wx.showToast({
            title: data.msg,
          })
        }
      }
    })


  },


  bindInputName: function (e) {
    this.setData({
      inputUserName: e.detail.value
    })
  },

  bindInputPhone: function (e) {
    this.setData({
      inputUserPhone: e.detail.value
    })
  },

  bindInputUnit: function (e) {
    this.setData({
      inputUserUnit: e.detail.value
    })
  },
  //返回上级页面
  goBack: function () {
    wx.navigateBack({
      delta: 1
    })
  }


})